package io.github.iogogogo.scheduled.support;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by tao.zeng on 2024/11/19.
 */
@Slf4j
public class XxlJobHttpConfig implements CookieJar {

    /**
     * cookie标识
     */
    public final static String COOKIE_KEY = "XXL_JOB_LOGIN_IDENTITY";

    /**
     * 临时存储cookie
     */
    public final static Map<String, Cookie> LOGIN_COOKIE = new HashMap<>(8);

    private enum SingletonEnum {
        INSTANCE;

        private final XxlJobHttpConfig singletonPattern;

        SingletonEnum() {
            singletonPattern = new XxlJobHttpConfig();
        }

        public XxlJobHttpConfig getInstance() {
            return singletonPattern;
        }
    }

    public static XxlJobHttpConfig getInstance() {
        return SingletonEnum.INSTANCE.getInstance();
    }


    @NotNull
    @Override
    public List<Cookie> loadForRequest(@NotNull HttpUrl httpUrl) {
        Cookie cookie = LOGIN_COOKIE.get(COOKIE_KEY);
        if (Objects.nonNull(cookie))
            log.debug("xxl-job cookie: {}", String.join("=", cookie.name(), cookie.value()));
        return Objects.nonNull(cookie) ? List.of(cookie) : List.of();
    }

    @Override
    public void saveFromResponse(@NotNull HttpUrl httpUrl, @NotNull List<Cookie> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            list.stream()
                    .filter(x -> x.name().equals(COOKIE_KEY))
                    .findFirst()
                    .ifPresent(x -> LOGIN_COOKIE.put(x.name(), x));
        }
    }

    private CookieJar cookieJar;

    private Interceptor interceptor;

    private ConnectionPool connectionPool;


    private int readTimeout = 60, connectTimeout = 60, callTimeout = 60, writeTimeout = 60;

    public OkHttpClient getOkHttpClient() {

        OkHttpClient.Builder builder = new OkHttpClient()
                .newBuilder()
                .readTimeout(Duration.ofSeconds(readTimeout))
                .writeTimeout(Duration.ofSeconds(writeTimeout))
                .callTimeout(Duration.ofSeconds(callTimeout))
                .connectTimeout(Duration.ofSeconds(connectTimeout));

        if (Objects.nonNull(interceptor)) builder.addInterceptor(interceptor);
        if (Objects.nonNull(connectionPool)) builder.connectionPool(connectionPool);
        if (Objects.nonNull(cookieJar)) builder.cookieJar(cookieJar);

        return builder.build();
    }


    public XxlJobHttpConfig readTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
        return this;
    }

    public XxlJobHttpConfig writeTimeout(int writeTimeout) {
        this.writeTimeout = writeTimeout;
        return this;
    }

    public XxlJobHttpConfig callTimeout(int callTimeout) {
        this.callTimeout = callTimeout;
        return this;
    }

    public XxlJobHttpConfig connectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }

    public XxlJobHttpConfig cookieJar(CookieJar cookieJar) {
        this.cookieJar = cookieJar;
        return this;
    }

    public XxlJobHttpConfig addInterceptor(Interceptor interceptor) {
        this.interceptor = interceptor;
        return this;
    }

    public XxlJobHttpConfig connectionPool(ConnectionPool connectionPool) {
        this.connectionPool = connectionPool;
        return this;
    }
}
